package com.sudoku.SudokuV01;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.sudokuModel.grilleModele;
import com.sudokuModel.sudokuCase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by richard on 30/01/14.
 */

 class vGrille
{
    public int num;
    public int done;
    public double level;

    public vGrille(int  n, int d, double level)
    {
        num = n;
        done = d;
        this.level = level;
    }

}
public class level1List extends Activity implements AdapterView.OnItemClickListener {

    ArrayList<vGrille> items;
    void initItems(double level){
        items = new ArrayList();

        for (int i=0; i < 100 ; i ++){
            vGrille crt = new vGrille(i,Math.round((float) (Math.random() * 100)),level);
            items.add(crt);
        }

    }


    public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        int itemPosition     = position;

        Activity lecontext = this;



        //grilleModele g = new grilleModele();
        //g.setTestGrille();
       // Log.w("MON MESS " ,  g.getDescription());
        Intent defineIntent = new Intent(lecontext, grilleDetail.class);
       // defineIntent.putExtra("personne", personnes[position]);
        lecontext.startActivity(defineIntent);

    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listegrille);


        Bundle objetbunble =  this.getIntent().getExtras();
        //personne result = objetbunble.getObject("personne");
        double level = objetbunble.getDouble("level");

        initItems(level);
        final ListView grilleList =(ListView) findViewById (R.id.vgrille);
      //version O items est remplacé par test
      //  String test[]={"toto" ,"titi"};

       // VErsion 1 adapater prend une liste d'objet mais ça s'affiche mal -> specialiser l'adapter
       // ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_2,android.R.id.text1,items);

        //grilleList.setAdapter(adapter);

        //version 3 ajout comportement

        grilleList.setOnItemClickListener(this);


        //Version 2
        grilleList.setAdapter(new MyAdapter(this, items));

    }
}

class MyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<vGrille> items;

    public MyAdapter(Context context, ArrayList<vGrille> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TwoLineListItem twoLineListItem;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            twoLineListItem = (TwoLineListItem) inflater.inflate(
                    android.R.layout.simple_list_item_2, null);
        } else {
            twoLineListItem = (TwoLineListItem) convertView;
        }

        TextView text1 = twoLineListItem.getText1();
        TextView text2 = twoLineListItem.getText2();

        text1.setText(items.get(position).num + "   niveau:" + (double) items.get(position).level);
        text2.setText(items.get(position).done+" %");
        text2.setTextSize(22);
        if (items.get(position).done < 40)
            text2.setTextColor(Color.RED);
        else
            text2.setTextColor(Color.GREEN);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Munro.ttf");
        text2.setTypeface(typeface);

        return twoLineListItem;




    }
}